# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#

validate_acct_info = (form) ->
    acct_name = form.form 'get value', 'acct_name'
    cred_no   = form.form 'get value', 'acct_cred_no'
    cred_type = form.form 'get value', 'acct_cred_type'

    # 如果开户人证件类型为身份证且身份证号码不为空，则需要验证身份证号码有效性
    if parseInt(cred_type) == 1 and cred_no.trim().length > 0
        result = validate_idno(cred_no, '收款账户开户人')
        if not result.succ
            alert result.errmsg
            return false

    # 账户姓名和证件号码必须提供一个
    if cred_no.trim().length == 0 and acct_name.trim().length == 0
        alert '账户开户人姓名和证件号码必须至少填写一个'
        return false

    # 验证卡号，不一定完全准确，因此只能给用户提示
    acct_no = form.form 'get value', 'acct_no'
    if acct_no.length > 0
        if not validate_cardno acct_no
            if not confirm "收款账号可能存在错误，选择［取消］按钮返回检查，或［确定］按钮继续提交."
                return false

    return true


$ ->
    form = $('form.main')
    form.form
        inline: true,
        fields:
            # 消费代付复核页面验证元素
            t20_merno:   optional: false, rules: [ type: 'empty' ]
            t20_termid:  optional: false, rules: [ type: 'empty' ]
            tz0_merno:   optional: false, rules: [ type: 'minLength[4]' ]
            tz0_termid:  optional: false, rules: [ type: 'empty' ]
            tz0_macalgo: optional: false, rules: [ type: 'empty' ]
            tz0_tmk:     optional: false, rules: [
                { type: 'minLength[32]' }, { type: 'maxLength[32]' }
            ]
            t20_fee:        optional: false, rules: [ type: 'empty' ]

            # 助农取款代付复核页面验证元素
            t60_merno:   optional: false, rules: [ type: 'empty' ]
            t60_termid:  optional: false, rules: [ type: 'empty' ]
            tz1_merno:   optional: false, rules: [ type: 'minLength[4]' ]
            tz1_termid:  optional: false, rules: [ type: 'empty' ]
            tz1_macalgo: optional: false, rules: [ type: 'empty' ]
            tz1_tmk:     optional: false, rules: [
                { type: 'minLength[32]' }, { type: 'maxLength[32]' }
            ]
            t60_fee:        optional: false, rules: [ type: 'empty' ]
            p2_fund:        optional: false, rules: [ type: 'empty' ]
            fee_take_type:  optional: false, rules: [ type: 'empty' ]
            fund_lack_proc: optional: false, rules: [ type: 'empty' ]

            name:        optional: false, rules: [ type: 'empty' ]
            contact:     optional: false, rules: [ type: 'empty' ]
            mobile:      optional: true,  rules: [ type: 'integer[..]' ]
            address:     optional: false, rules: [ type: 'empty' ]
            email:       optional: true,  rules: [ type: 'email' ]

            acct_no:        optional: false, rules: [
                { type: 'empty' }, { type: 'integer[..]' },
            ]
            acct_cred_type: optional: true,  rules: [ type: 'integer[..]' ]
            acct_mobile:    optional: true,  rules: [ type: 'integer[..]' ]
            pay_chnl:       optional: false, rules: [ type: 'empty' ]
            pay_merid:      optional: false, rules: [ type: 'empty' ]

    freeze_form form

    # 页面数据集
    record = form.form 'get value', 'record'
    if record and record.trim().length > 0
        form.form 'set value', 'record', ''
        record = JSON.parse record
        form.form 'set values', record

    # 拒绝按钮事件
    form.find('button#reject').click ->
        if $(this).hasClass('has_clicked')
            reason = form.form 'get value', 'reject_reason'
            if not reason or reason.trim().length == 0
                alert '请输入拒绝的原因，以便录入人员可以有针对性的进行修改'
                return
            $(this).removeClass('has_clicked').text('拒 绝')
            action = form.attr 'action'
            form.attr 'action', action + '/reject'
            form.submit()
            form.attr 'action', action
        else
            form.find('button#pass').text('通 过').removeClass('has_clicked')
            form.find('.fields#effect_time').hide()
            form.find('.fields#reject_reason').show()
            $(this).addClass('has_clicked').text('确认拒绝')

    # 通过按钮事件
    form.find('button#pass').click ->
        return if not form.form 'is valid'

        # 消费代付和助农代付复核时都需要验证账户信息
        return if not validate_acct_info form

        if $(this).hasClass('has_clicked')
            effect_time = form.form 'get value', 'effect_time'
            if not effect_time or effect_time.trim().length == 0
                alert '请选择信息生效的时间'
                return
            $(this).removeClass('has_clicked').text('通 过')
            action = form.attr 'action'
            form.attr 'action', action + '/pass'
            form.submit()
            form.attr 'action', action
        else
            form.find('button#reject').text('拒 绝').removeClass('has_clicked')
            form.find('.fields#reject_reason').hide()
            form.find('.fields#effect_time').show()
            $(this).addClass('has_clicked').text('确认通过')
